package com.huige.wiki.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    private static final Logger LOG = LoggerFactory.getLogger(RedisUtil.class);

    @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate redisTemplate;

    /**
     * 判断redis是否已经存在key，不存在则存入redis
     * @param key
     * @param second  过期时间
     * @return
     */
    public boolean validateRepeat(String key,long second){
        if(redisTemplate.hasKey(key)){
            LOG.info("key已存在：{}",key);
            return false;
        }else{
            redisTemplate.opsForValue().set(key,key,second, TimeUnit.SECONDS);
            return true;
        }
    }
}
